package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Slf4j
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;
    //从服务中心订阅数据

    @RequestMapping("/findPage")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("获取到参数++++++++"+queryPageBean);
        PageResult pageResult = checkItemService.findPage(queryPageBean);
        log.debug("查询数据成功++++++++👆");
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,pageResult);
    };


    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem){
        log.debug("Controller获得了前端传入的添加数据参数+++++++++++"+checkItem);
        checkItemService.add(checkItem);
        log.debug("检查项添加成功！！+++++++++++");
        return new Result(true, MessageConst.ADD_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/delById")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result delById(Integer id){
        log.debug("获得要删除的id值+++++++++++++"+id);
        checkItemService.del(id);
        log.debug("删除成功！！！！！++++++++++++");
        return new Result(true,MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY','CHECKITEM_EDIT')")
    public Result findById(Integer id){
        log.debug("获得根据id查询的id值+++++++++++"+id);
        CheckItem byId = checkItemService.findById(id);
        log.debug("成功根据id查询到数据+++++++++++");
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,byId);
    };

    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody CheckItem checkItem) {
        log.debug("获得了需要更改的数据+++++++++++"+checkItem);
        checkItemService.editById(checkItem);
        log.debug("更改数据成功！！+++++++++++++++");
        return new Result(true,MessageConst.EDIT_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY','CHECKITEM_EDIT')")
    public Result findAll(){
        List all = checkItemService.findAll();
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,all);
    }


}
